<?php
/**
 * Created by PhpStorm.
 * User: zyh
 * Date: 2018/5/9
 * Time: 上午10:00
 */

namespace app\admin\controller;
use app\admin\model\Admin;
use GatewayWorker\Lib\Gateway;
use think\Controller;
use think\Session;

class Auth extends Controller
{
    public function login()
    {
        $status = ['stauts'=>1,'error'=>''];
        if ($arr = input('post.')){

            $admin = new Admin();
            if ($admin->login($arr)){
                $this->redirect(url("admin/index/index"));
            }else{
                $status = ['status'=>0,'error'=>'用户名或密码错误'];
            }
        }
        $this->assign("system",\app\admin\model\System::getSystem());
        $this->assign("status",$status);
        return view();
    }

    public function logout()
    {
        Session::set('login',false);
        $this->redirect('auth/login');
    }

    public function bind($clientid)
    {
        Gateway::bindUid($clientid,"admin");
        //加入admin群组
        Gateway::joinGroup($clientid,"admin");
    }

    public function changepwd()
    {
        if ($pwd = input('post.')){
            if ($pwd['password'] == $pwd['password1']){
                $admin = Admin::get(1);
                $admin['password'] = md5($pwd['password']);
                $admin->save();
                return json(['status'=>1]);
            }else{
                return json(['status'=>0]);
            }
        }
        return view();
    }
}